package xyz.yuzegod.megawalls.command;

import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.LPRankProvider;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.Game;
import xyz.yuzegod.megawalls.game.GamePlayer;

public class ShoutCommand extends BaseCommand
{
    public ShoutCommand() {
        super("shout");
        this.setPermission("MegaWalls.default");
    }

    @Override
    public String getPossibleArguments() {
        return "";
    }

    @Override
    public int getMinimumArguments() {
        return 0;
    }

    @Override
    public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
        final Game game = MegaWalls.getInstance().getGame();
        if (game == null) {
            sender.sendMessage("§c§l未获取到游戏对象！");
            return;
        }
        if (args.length<=0){
            return;
        }
        String result;
        if (args.length==1){
            result=args[0];
        }else {
            StringBuilder sb = new StringBuilder();
            for (String s : args) {
                sb.append(s);
                sb.append(" ");
            }
            result=sb.toString();
        }
        if (game.isStarted()) {
            final GamePlayer gamePlayer = GamePlayer.get(((Player)sender).getUniqueId());
            if (gamePlayer.isSpectator()) {
                return;
            }
            result=("§6[喊话] " + gamePlayer.getGameTeam().getTeamColor().getChatPrefix() + " " + LPRankProvider.getPrefix(gamePlayer.getPlayer()) + gamePlayer.getPlayer().getDisplayName() + LPRankProvider.getSuffix(gamePlayer.getPlayer()) + "§f： " + result);
            for (GamePlayer viewer:GamePlayer.getOnlinePlayers()) {
                viewer.getPlayer().sendMessage(result);
            }
        }
    }

    @Override
    public boolean isOnlyPlayerExecutable() {
        return true;
    }
}
